﻿namespace UnityEditor
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using UnityEditor.VisualStudioIntegration;
    using UnityEngine;

    internal class PackageExport : EditorWindow
    {
        [CompilerGenerated]
        private static Func<AssetsItem, bool> <>f__am$cache7;
        [CompilerGenerated]
        private static Func<FilteredHierarchy.FilterResult, string> <>f__am$cache8;
        [CompilerGenerated]
        private static Dictionary<string, int> <>f__switch$map11;
        [SerializeField]
        private AssetsItem[] m_assets;
        [SerializeField]
        private bool m_bIncludeDependencies = true;
        [SerializeField]
        private int m_LeastIndent = 0xf423f;
        [SerializeField]
        private ListViewState m_ListView = new ListViewState(0, 0x12);
        private static readonly string[] m_ScriptClassNames = new string[] { "monoscript" };
        private static Constants ms_Constants;

        public PackageExport()
        {
            base.position = new Rect(100f, 100f, 400f, 300f);
            base.minSize = new Vector2(400f, 200f);
        }

        private void BuildAssetList()
        {
            HashSet<string> source = new HashSet<string>(Selection.assetGUIDs);
            if (source.Count == 0)
            {
                string[] collection = new string[0];
                source = new HashSet<string>(AssetServer.CollectAllChildren(AssetServer.GetRootGUID(), collection));
            }
            this.m_assets = AssetServer.BuildExportPackageAssetListAssetsItems(source.ToArray<string>(), this.m_bIncludeDependencies);
            if (this.m_bIncludeDependencies)
            {
                if (<>f__am$cache7 == null)
                {
                    <>f__am$cache7 = asset => IsScriptOrAssembly(asset.pathName);
                }
                if (Enumerable.Any<AssetsItem>(this.m_assets, <>f__am$cache7))
                {
                    this.m_assets = AssetServer.BuildExportPackageAssetListAssetsItems(source.Union<string>(GetAllScriptGUIDs()).ToArray<string>(), this.m_bIncludeDependencies);
                }
            }
        }

        private void CheckChildren(AssetsItem parentAI)
        {
            foreach (AssetsItem item in this.m_assets)
            {
                if (item.parentGuid == parentAI.guid)
                {
                    item.enabled = parentAI.enabled;
                    this.CheckChildren(item);
                }
            }
        }

        private static int CountOccurencesOfChar(string instance, char c)
        {
            int num = 0;
            foreach (char ch in instance)
            {
                if (c == ch)
                {
                    num++;
                }
            }
            return num;
        }

        private void Export()
        {
            string fileName = EditorUtility.SaveFilePanel("Export package ...", string.Empty, string.Empty, "unitypackage");
            if (fileName != string.Empty)
            {
                List<string> list = new List<string>();
                foreach (AssetsItem item in this.m_assets)
                {
                    if (item.enabled != 0)
                    {
                        list.Add(item.guid);
                    }
                }
                AssetServer.ExportPackage(list.ToArray(), fileName);
                base.Close();
                GUIUtility.ExitGUI();
            }
        }

        private void FrameLastGUIRect()
        {
            Rect lastRect = GUILayoutUtility.GetLastRect();
            HandleUtility.handleWireMaterial.SetPass(0);
            GL.Begin(1);
            GL.Color(ms_Constants.lineColor);
            GL.Vertex3(lastRect.xMax + 1f, lastRect.y, 0f);
            GL.Vertex3(lastRect.xMax + 1f, lastRect.yMax, 0f);
            GL.Vertex3(lastRect.xMax + 1f, lastRect.yMax, 0f);
            GL.Vertex3(lastRect.x + 1f, lastRect.yMax, 0f);
            GL.Vertex3(lastRect.x + 1f, lastRect.yMax, 0f);
            GL.Vertex3(lastRect.x + 1f, lastRect.y, 0f);
            GL.End();
        }

        private static IEnumerable<string> GetAllScriptGUIDs()
        {
            SearchFilter filter = new SearchFilter {
                classNames = m_ScriptClassNames
            };
            FilteredHierarchy hierarchy = new FilteredHierarchy(HierarchyType.Assets) {
                searchFilter = filter
            };
            if (<>f__am$cache8 == null)
            {
                <>f__am$cache8 = result => AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(result.instanceID));
            }
            return Enumerable.Select<FilteredHierarchy.FilterResult, string>(hierarchy.results, <>f__am$cache8);
        }

        private static bool IsScriptOrAssembly(string filename)
        {
            string extension = Path.GetExtension(filename);
            if (extension != null)
            {
                int num;
                if (<>f__switch$map11 == null)
                {
                    Dictionary<string, int> dictionary = new Dictionary<string, int>(5);
                    dictionary.Add(".cs", 0);
                    dictionary.Add(".js", 0);
                    dictionary.Add(".boo", 0);
                    dictionary.Add(".dll", 1);
                    dictionary.Add(".exe", 1);
                    <>f__switch$map11 = dictionary;
                }
                if (<>f__switch$map11.TryGetValue(extension, out num))
                {
                    if (num == 0)
                    {
                        return true;
                    }
                    if (num == 1)
                    {
                        return SolutionSynchronizer.IsManagedAssembly(filename);
                    }
                }
            }
            return false;
        }

        public void OnGUI()
        {
            if (ms_Constants == null)
            {
                ms_Constants = new Constants();
            }
            if (this.m_assets != null)
            {
                if (this.m_LeastIndent == 0xf423f)
                {
                    int leastIndent = this.m_LeastIndent;
                    for (int i = 0; i < this.m_assets.Length; i++)
                    {
                        int num3 = CountOccurencesOfChar(this.m_assets[i].pathName, '/');
                        if (leastIndent > num3)
                        {
                            leastIndent = num3;
                        }
                    }
                    this.m_LeastIndent = leastIndent - 1;
                }
                if (this.m_assets != null)
                {
                    this.SetupListView();
                    bool flag = Event.current.type == EventType.Repaint;
                    GUILayout.BeginVertical(new GUILayoutOption[0]);
                    GUILayout.Label("Items to Export", ms_Constants.title, new GUILayoutOption[0]);
                    GUILayout.Space(1f);
                    EditorGUIUtility.SetIconSize(new Vector2(16f, 16f));
                    IEnumerator enumerator = ListViewGUI.ListView(this.m_ListView, GUIStyle.none, new GUILayoutOption[0]).GetEnumerator();
                    try
                    {
                        while (enumerator.MoveNext())
                        {
                            ListViewElement current = (ListViewElement) enumerator.Current;
                            AssetsItem parentAI = this.m_assets[current.row];
                            Rect position = current.position;
                            position = new Rect(position.x + 1f, position.y, position.width - 2f, position.height);
                            int num4 = CountOccurencesOfChar(parentAI.pathName, '/') - this.m_LeastIndent;
                            if (flag && (this.m_ListView.row == current.row))
                            {
                                ms_Constants.ConsoleEntryBackEven.Draw(position, false, false, true, false);
                            }
                            float y = current.position.y;
                            position.x += 3f;
                            int enabled = parentAI.enabled;
                            parentAI.enabled = !GUI.Toggle(new Rect(position.x, position.y, 16f, 16f), parentAI.enabled != 0, string.Empty) ? 0 : 1;
                            if (enabled != parentAI.enabled)
                            {
                                this.m_ListView.row = current.row;
                                GUIUtility.keyboardControl = this.m_ListView.ID;
                                this.CheckChildren(parentAI);
                            }
                            if (flag)
                            {
                                Rect rect2 = new Rect(position.x + (15 * num4), y + 1f, 16f, 16f);
                                GUI.DrawTexture(rect2, AssetDatabase.GetCachedIcon(parentAI.pathName));
                            }
                            position = new Rect((position.x + 20f) + (15 * num4), current.position.y, position.width - (20 + (15 * num4)), position.height);
                            GUI.Label(position, parentAI.pathName);
                        }
                    }
                    finally
                    {
                        IDisposable disposable = enumerator as IDisposable;
                        if (disposable == null)
                        {
                        }
                        disposable.Dispose();
                    }
                    this.FrameLastGUIRect();
                    GUILayout.EndVertical();
                    if (((this.m_ListView.row != -1) && (GUIUtility.keyboardControl == this.m_ListView.ID)) && ((Event.current.type == EventType.KeyDown) && (Event.current.keyCode == KeyCode.Space)))
                    {
                        this.m_assets[this.m_ListView.row].enabled = (this.m_assets[this.m_ListView.row].enabled != 0) ? 0 : 1;
                        this.CheckChildren(this.m_assets[this.m_ListView.row]);
                        Event.current.Use();
                    }
                    EditorGUIUtility.SetIconSize(Vector2.zero);
                    GUILayout.Space(5f);
                    GUILayout.BeginHorizontal(new GUILayoutOption[0]);
                    GUILayout.Space(10f);
                    GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.Width(50f) };
                    if (GUILayout.Button(EditorGUIUtility.TextContent("All"), options))
                    {
                        for (int j = 0; j < this.m_assets.Length; j++)
                        {
                            this.m_assets[j].enabled = 1;
                        }
                    }
                    GUILayoutOption[] optionArray2 = new GUILayoutOption[] { GUILayout.Width(50f) };
                    if (GUILayout.Button(EditorGUIUtility.TextContent("None"), optionArray2))
                    {
                        for (int k = 0; k < this.m_assets.Length; k++)
                        {
                            this.m_assets[k].enabled = 0;
                        }
                    }
                    GUILayout.Space(10f);
                    bool flag2 = GUILayout.Toggle(this.m_bIncludeDependencies, "Include dependencies", new GUILayoutOption[0]);
                    if (flag2 != this.m_bIncludeDependencies)
                    {
                        this.m_bIncludeDependencies = flag2;
                        this.BuildAssetList();
                    }
                    GUILayout.FlexibleSpace();
                    if (GUILayout.Button(EditorGUIUtility.TextContent("Export..."), new GUILayoutOption[0]))
                    {
                        this.Export();
                        GUIUtility.ExitGUI();
                    }
                    GUILayout.Space(10f);
                    GUILayout.EndHorizontal();
                    GUILayout.Space(10f);
                }
            }
        }

        private void SetupListView()
        {
            if (this.m_assets != null)
            {
                this.m_ListView.totalRows = this.m_assets.Length;
            }
        }

        private static void ShowExportPackage()
        {
            PackageExport window = EditorWindow.GetWindow<PackageExport>(true, "Exporting package");
            window.BuildAssetList();
            window.Repaint();
        }

        internal class Constants
        {
            public GUIStyle ConsoleEntryBackEven = "CN EntryBackEven";
            public GUIStyle ConsoleEntryBackOdd = "CN EntryBackOdd";
            public Color lineColor = ((EditorGUIUtility.skinIndex == 0) ? new Color(0.4f, 0.4f, 0.4f) : new Color(0.1f, 0.1f, 0.1f));
            public GUIStyle title = "OL Title";
        }
    }
}

